package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StatementItemServiceImpl implements IStatementItemService {
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        // 获取最后一条数据
        StatementItem tempItem = items.remove(items.size() - 1);
        // 获取前台传入的折扣金额(获取的是最后一个元素的放在服务单价上的价格)
        BigDecimal disCountPrice = tempItem.getItemPrice();
        // 进行合理性校验，只有在消费状态下才可以进行数据的保存
        // 根据对象获取id
        Long statementId = tempItem.getStatementId();
        // 根据id获取结算单对象
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("已支付的结算单不能保存明细");
        }
        // 在把明细项里面的数据删除掉
        statementItemMapper.deleteByStatementId(statementId);
        // 定义计算整个明细的总金额，总商品数量
        BigDecimal totalAmount = new BigDecimal("0.0");
        BigDecimal totalQuantity = new BigDecimal("0");
        for (StatementItem item : items) {
            // 往结算明细表中添加数据
            statementItemMapper.insert(item);
            // 计算总金额和总商品总数量
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        // 总金额 - 优惠价格 < 0
        if (totalAmount.compareTo(disCountPrice) < 0){
            throw new BusinessException("优惠价格不能大于总金额");
        }
        // 往结算表中添加数据
        statementService.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    @Transactional
    public void updatePayStatus(Long statementId) {
        // 进行合理性校验
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("已支付的结算单不能重复支付");
        }
        // 更新结算单的状态
        // ShiroUtils.getUserId()表示通过工具类获取收款人的id
        statementService.updatePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());

        if (statement.getAppointmentId() != null){
            // 表示是预约单生成的结算单数据,修改预约单中的状态
            appointmentService.updateStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }
}
